using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
namespace BMC.ARSystem
{
	public class XMLDataExporter : DataExporter
	{
		protected override object CreateWriter(string filePath, Encoding encoding, bool isAppendMode)
		{
			XmlTextWriter xmlTextWriter = new XmlTextWriter(filePath, encoding);
			xmlTextWriter.set_Formatting(1);
			xmlTextWriter.set_Indentation(1);
			xmlTextWriter.set_IndentChar('\t');
			return xmlTextWriter;
		}
		protected override void CloseWriter(object writer)
		{
			((XmlTextWriter)writer).Close();
		}
		protected override void WriteHeader(string formName, string serverCharSet, ArrayList fieldNames, Hashtable fieldsMap, object writer)
		{
			XmlTextWriter xmlTextWriter = (XmlTextWriter)writer;
			xmlTextWriter.WriteStartDocument();
			xmlTextWriter.WriteStartElement("arsystem_xml_report");
			xmlTextWriter.WriteStartElement("xform");
			xmlTextWriter.WriteAttributeString("id", formName);
			xmlTextWriter.WriteAttributeString("xmlns", "http://www.w3.org/2000/xforms");
			xmlTextWriter.WriteStartElement("model");
			xmlTextWriter.WriteStartElement("group");
			xmlTextWriter.WriteAttributeString("name", "field-info");
			xmlTextWriter.WriteAttributeString("minOccurs", "1");
			xmlTextWriter.WriteAttributeString("maxOccurs", "1");
			xmlTextWriter.WriteStartElement("group");
			xmlTextWriter.WriteAttributeString("name", "field");
			xmlTextWriter.WriteAttributeString("minOccurs", "1");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "name");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("number");
			xmlTextWriter.WriteAttributeString("name", "id");
			xmlTextWriter.WriteAttributeString("long", "true");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "type");
			xmlTextWriter.WriteAttributeString("range", "closed");
			xmlTextWriter.WriteElementString("value", "CHAR");
			xmlTextWriter.WriteElementString("value", "TIME");
			xmlTextWriter.WriteElementString("value", "DECIMAL");
			xmlTextWriter.WriteElementString("value", "REAL");
			xmlTextWriter.WriteElementString("value", "INT");
			xmlTextWriter.WriteElementString("value", "ENUM");
			xmlTextWriter.WriteElementString("value", "ATTACH");
			xmlTextWriter.WriteElementString("value", "DIARY");
			xmlTextWriter.WriteElementString("value", "TIMEOFDAY");
			xmlTextWriter.WriteElementString("value", "DATE");
			xmlTextWriter.WriteElementString("value", "CURRENCY");
			xmlTextWriter.WriteElementString("value", "NULL");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("group");
			xmlTextWriter.WriteAttributeString("name", "entry");
			xmlTextWriter.WriteAttributeString("minOccurs", "1");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteStartElement("group");
			xmlTextWriter.WriteAttributeString("name", "field_value");
			xmlTextWriter.WriteAttributeString("minOccurs", "1");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "value");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("group");
			xmlTextWriter.WriteAttributeString("name", "status_history");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "date");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "user");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("group");
			xmlTextWriter.WriteAttributeString("name", "currency");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "currency_value");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "currency_type");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "currency_date");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "currency_functional");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("group");
			xmlTextWriter.WriteAttributeString("name", "diary");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "date");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "user");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("string");
			xmlTextWriter.WriteAttributeString("name", "diary_content");
			xmlTextWriter.WriteAttributeString("maxOccurs", "*");
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteStartElement("instance");
			xmlTextWriter.WriteStartElement("field-info");
			IEnumerator enumerator = fieldNames.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					string text = (string)enumerator.get_Current();
					xmlTextWriter.WriteStartElement("field");
					if (((Field)fieldsMap.get_Item(text)).Id != 15u)
					{
						xmlTextWriter.WriteElementString("name", text);
					}
					else
					{
						xmlTextWriter.WriteElementString("name", DataExporter.LABEL_STATUS_HISTORY_DEFAULT_LABEL);
					}
					xmlTextWriter.WriteElementString("id", ((Field)fieldsMap.get_Item(text)).Id.ToString());
					xmlTextWriter.WriteElementString("type", ARXDataTypeWriter.GetDataTypeString(((Field)fieldsMap.get_Item(text)).DataType));
					xmlTextWriter.WriteEndElement();
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			xmlTextWriter.WriteEndElement();
		}
		protected override void WriteRecord(Server server, object writer, string formName, ArrayList fieldNames, Hashtable fieldsMap, EntryFieldValue entry, string attachValAbsFolder, string attachValRelFolder, ref bool hasAttachVal, ref int attachValCount, ref uint numExported, ref uint numFailed, ref uint numSkipped)
		{
			XmlTextWriter xmlTextWriter = writer as XmlTextWriter;
			xmlTextWriter.WriteStartElement("entry");
			IEnumerator enumerator = fieldNames.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					string text = (string)enumerator.get_Current();
					Field field = fieldsMap.get_Item(text) as Field;
					object obj = entry.FieldValues[field.Id];
					uint dataType = field.DataType;
					string text2 = "";
					xmlTextWriter.WriteStartElement("field_value");
					if (field.Id == 102u || field.Id == 123u)
					{
						text2 = server.SecondOperationTime(obj, field.Id);
					}
					else
					{
						if (obj != null && obj != DBNull.Value && obj != Missing.Value)
						{
							if (field.Id == 15u)
							{
								StatusHistory statusHistory = obj as StatusHistory;
								if (statusHistory == null)
								{
									goto IL_422;
								}
								DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0);
								StatusHistory.Enumerator enumerator2 = statusHistory.GetEnumerator();
								try
								{
									while (enumerator2.MoveNext())
									{
										StatusHistoryItem current = enumerator2.Current;
										xmlTextWriter.WriteStartElement("status_history");
										if (current.Time != DateTime.MaxValue)
										{
											xmlTextWriter.WriteElementString("date", (current.Time - dateTime).get_TotalSeconds().ToString());
											xmlTextWriter.WriteElementString("user", current.User);
										}
										xmlTextWriter.WriteEndElement();
									}
									goto IL_422;
								}
								finally
								{
									IDisposable disposable = enumerator2 as IDisposable;
									if (disposable != null)
									{
										disposable.Dispose();
									}
								}
							}
							if (field is AttachmentField && obj is Attachment)
							{
								xmlTextWriter.WriteStartElement("attach_value");
								xmlTextWriter.WriteStartElement("attach_loc");
								xmlTextWriter.WriteElementString("loc_type", "1");
								hasAttachVal = true;
								Util.EnsureDirExists(attachValAbsFolder);
								string text3 = ((Attachment)obj).Name;
								string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text3);
								string extension = Path.GetExtension(text3);
								string text4;
								text3 = Path.Combine(attachValAbsFolder, text4 = string.Format("{0}_{1}{2}", fileNameWithoutExtension, attachValCount, extension));
								server.GetEntryBLOB(formName, entry.EntryId, field.Id, text3);
								DataExporter.ExportAttachmentValueEventArgs e = new DataExporter.ExportAttachmentValueEventArgs(numExported, entry, field.Id, attachValAbsFolder, text4);
								this.OnExportAttachmentValueEvent(e);
								xmlTextWriter.WriteElementString("filename", Path.Combine(attachValRelFolder, text4));
								xmlTextWriter.WriteEndElement();
								xmlTextWriter.WriteEndElement();
								attachValCount++;
							}
							else
							{
								if (field.DataType == 12u)
								{
									CurrencyValue currencyValue = (CurrencyValue)obj;
									xmlTextWriter.WriteStartElement("currency");
									xmlTextWriter.WriteElementString("currency_value", currencyValue.Value.ToString());
									xmlTextWriter.WriteElementString("currency_type", currencyValue.CurrencyCode);
									xmlTextWriter.WriteElementString("currency_date", Util.GetARTimestamp(currencyValue.ConversionDate).ToString());
									string text5 = currencyValue.FunctionalCurrencies.get_Count().ToString();
									IEnumerator enumerator3 = currencyValue.FunctionalCurrencies.GetEnumerator();
									try
									{
										while (enumerator3.MoveNext())
										{
											object[] array = (object[])enumerator3.get_Current();
											text5 += string.Format(" {0} {1}", array[0], array[1]);
										}
									}
									finally
									{
										IDisposable disposable = enumerator3 as IDisposable;
										if (disposable != null)
										{
											disposable.Dispose();
										}
									}
									xmlTextWriter.WriteElementString("currency_functional", text5);
									xmlTextWriter.WriteEndElement();
								}
								else
								{
									if (field is DiaryField)
									{
										DateTime dateTime2 = new DateTime(1970, 1, 1, 0, 0, 0);
										DiaryList diaryList = obj as DiaryList;
										DiaryList.Enumerator enumerator4 = diaryList.GetEnumerator();
										try
										{
											while (enumerator4.MoveNext())
											{
												DiaryEntry current2 = enumerator4.Current;
												xmlTextWriter.WriteStartElement("diary");
												xmlTextWriter.WriteElementString("date", (current2.Time - dateTime2).get_TotalSeconds().ToString());
												xmlTextWriter.WriteElementString("user", current2.User);
												xmlTextWriter.WriteElementString("diary_content", XMLCharWriter.EscapeString(current2.Text));
												xmlTextWriter.WriteEndElement();
											}
											goto IL_422;
										}
										finally
										{
											IDisposable disposable = enumerator4 as IDisposable;
											if (disposable != null)
											{
												disposable.Dispose();
											}
										}
									}
									XMLDataTypeWriter xMLDataTypeWriter = XMLDataTypeWriter.CreateWriter(dataType);
									text2 = xMLDataTypeWriter.WriteValue(obj);
								}
							}
						}
					}
					IL_422:
					if (text2.get_Length() > 0)
					{
						xmlTextWriter.WriteElementString("value", text2);
					}
					xmlTextWriter.WriteEndElement();
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			xmlTextWriter.WriteEndElement();
		}
		protected override void WriteFooter(object writer)
		{
			XmlTextWriter xmlTextWriter = (XmlTextWriter)writer;
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
			xmlTextWriter.WriteEndElement();
		}
	}
}
